package www.wen.com.common.lib.view.popuview

import android.content.Context
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import androidx.annotation.NonNull
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.extension.getColorAgency
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.ResultUiCallBack
import www.wen.com.common.lib.util.SpanUtils

/**
 * @author WEN
 * @Description:协议的
 * @date 2021/3/3 14:53
 */
class AgreementPopupView(
    context: Context,
    private val resultAgreementCallBack: ResultUiCallBack<String>? = null,
    private val resultCallBack: ResultUiCallBack<String>? = null,
) : CenterPopupView(context), View.OnClickListener {


    lateinit var tvContent: TextView

    lateinit var tvNotUpDateApp: TextView

    lateinit var tvUpDateApp: TextView


    override fun getImplLayoutId(): Int = R.layout.popupview_agreement

    override fun onClick(v: View) {
        resultCallBack?.invoke(v, "", this)
    }

    override fun initPopupContent() {
        super.initPopupContent()
        tvNotUpDateApp = findViewById(R.id.tvNotUpDateApp)
        tvUpDateApp = findViewById(R.id.tvUpDateApp)
        tvContent = findViewById(R.id.tvContent)

        SpanUtils.with(tvContent)
            .append(
                "请你务必审慎阅读，充分理解“服务协议”和“隐私政策”各条款，包括但不限于：为了向你提供即时通讯、内容分享等服务，我们需要收集你的设备信息、操作日志等个人信息。你可以在“设置”中查看、变更、删除个人信息并管理你的授权。\n" +
                        "你可阅读"
            )
            .setForegroundColor(getColorAgency(R.color.colorFFFFFF))
            .append("《用户协议》")
            .setForegroundColor(getColorAgency(R.color.color6264C8))
            .setClickSpan(object : ClickableSpan() {

                override fun onClick(widget: View) {
                    tvContent.highlightColor = getColorAgency(android.R.color.transparent)
                    resultAgreementCallBack?.invoke(tvContent, "用户协议", this@AgreementPopupView)
                }

                override fun updateDrawState(@NonNull ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = getColorAgency(R.color.color6264C8)
                    ds.isUnderlineText = false
                }

            })
            .append("和")
            .setForegroundColor(getColorAgency(R.color.colorFFFFFF))
            .append("《隐私政策》")
            .setForegroundColor(getColorAgency(R.color.color6264C8))
            .setClickSpan(object : ClickableSpan() {

                override fun onClick(widget: View) {
                    tvContent.highlightColor = getColorAgency(android.R.color.transparent)
                    resultAgreementCallBack?.invoke(tvContent, "隐私政策", this@AgreementPopupView)
                }

                override fun updateDrawState(@NonNull ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = getColorAgency(R.color.color6264C8)
                    ds.isUnderlineText = false
                }

            })
            .append("了解详细信息。如你同意，请点击“同意”开始接收我们的服务。")
            .setForegroundColor(getColorAgency(R.color.colorFFFFFF))
            .create()
        setOnClickListenerAgency(this, tvNotUpDateApp, tvUpDateApp)
    }

    override fun getMaxWidth(): Int {
        return (XPopupUtils.getWindowWidth(context) * 0.8f).toInt()
    }


}


